<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
class Collection extends Api
{
    protected $with = ['script'=>['script_class.script_setting','script_setting_xs','script_setting_nd'],'user_index'];
    //用户端-剧本收藏【收藏-取消收藏】
    public function add()
    {
        $this->checkLogin();
        $post = param();
        //剧本ID
        if(!isset($post['id'])) return msgerror('参数缺失');
        $data = $this->model->where([
            ['user_index_id','=',$this->userinfo['id']],
            ['script_id','=',$post['id']]
        ])->find();
        if(!empty($data) && !empty($data->toArray()))
        {
            $data = $data->toArray();
            //存在-取消收藏
            $this->model::destroy($data['id']);
            return datasuccess([],'取消收藏');
        }else{
            //不存在-收藏
            $this->model->save([
                'user_index_id'=>$this->userinfo['id'],
                'script_id'=>$post['id']
            ]);
            return datasuccess([],'收藏成功');
        }
    }
    //用户端-个人中心-我的剧本收藏
    public function index()
    {
        $this->checkLogin();
        $post = param();
        $limit = $post['limit'] ?? 20;
        $data = $this->model->where([
            ['user_index_id','=',$this->userinfo['id']]
        ])->with($this->with)->paginate($limit);
        return datasuccess($data);
    }
}